import Vue from 'vue'
import App from './App.vue'
import Element from 'element-ui'
import router, {routes} from './router'
import store from './store'
import AX from '@/utils/AX.js' 

import 'font-awesome/css/font-awesome.min.css'
import 'element-ui/lib/theme-chalk/index.css'
import '@/common/less/base.less'

import '@/mock/mock.js'

Vue.use(Element)
Vue.config.productionTip = false
Vue.prototype.ax = AX

router.beforeEach((to, from, next) => {
  // 提取路由配置，生成侧边栏菜单列表
  const toPath = to.matched[0].path
  const fromRoute = from.matched[0]
  if (!fromRoute || toPath !== fromRoute.path) {
    const results = routes.filter(r => r.path === toPath)
    store.commit('updateMenu', results[0])
  }

  // token验证
  const user = localStorage.getItem('G:token')
  if (to.matched.some(record => record.meta.requiresAuth)) {
    next()
  } else {
    if (user) {
      next()
    } else {
      next({ path: '/login' })
    }
  }
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
